package com.starbaba.spread.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.controllog.aop.LogAble;
import com.starbaba.controllog.model.ControlLogCategory;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.constant.WebConstants;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.spread.comment.Constant;
import com.starbaba.spread.model.AdvertisingModel;
import com.starbaba.spread.service.AdvertisingService;
import com.starbaba.system.user.model.UserVO;
import com.starbaba.tools.sequence.Sequence;

/**
 * 广告投放控制器
 * 
 * @author Herz
 *
 */
@SuppressWarnings("serial")
public class AdvertisingAction extends BaseAction implements ModelDriven {

	@Resource(name = "advertisingServiceImpl")
	private AdvertisingService service;

	private ArrayList<ControlLogCategory> logCategorysArrayList = new ArrayList<ControlLogCategory>();
	private AdvertisingModel model = new AdvertisingModel();
	private final static String CM_CONTROL_ID = "1000001001";

	@Override
	public Object getModel() {
		return model;
	}

	public String list() {
		return "list";
	}

	/**
	 * 通过session 获取当前登陆人信息 判断是不是管理员
	 */
	public void ismanager() {
		UserVO userVO = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		if (userVO.getIsManager()) {
			sendMsg(Constant.TRUE);
		} else {
			sendMsg(Constant.FALSE);
		}
	}

	/**
	 * 条件列表查询
	 */
	public void alist() {
		parsePageRequest();
		try {
			PageResponse pageResponse = new PageResponse();
			Integer startPoint = pageRequest.getIDisplayStart();
			Integer displayLength = pageRequest.getIDisplayLength();

			if (null == model.getName()) {
				startPoint = 0;
				displayLength = 100000;
			}
			// 根据表单实体查询数据库列表
			List list = service.List(startPoint, displayLength, model, pageResponse);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			String jsonStr = JsonUtils.toJson(pageResponse, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			sendMsg(e.getMessage());
		}
	}

	/**
	 * 广告商具体pv uv详情
	 * 
	 */
	public void myList() {
		parsePageRequest();
		try {
			PageResponse pageResponse = new PageResponse();
			Integer startPoint = pageRequest.getIDisplayStart();
			Integer displayLength = pageRequest.getIDisplayLength();
			if (null == model.getName()) {
				startPoint = 0;
				displayLength = 100000;
			}
			List list = service.adlist(startPoint, displayLength, model, pageResponse);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			String jsonStr = JsonUtils.toJson(pageResponse, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			sendMsg(e.getMessage());
		}
	}

	/**
	 * 修改
	 */
	@LogAble(operate = LogAble.OPERATE_MODIFY)
	public void adEdit() {
		int num = service.update(model);
		try {
			if (num == 0) {
				sendMsg(Constant.UPDATE_FALSE);
			} else if (num == 1) {
				sendMsg(Constant.UPDATE_SUCCESS);
			}
			logWriter(Long.parseLong(model.getId()));
		} catch (Exception e) {
			sendMsg(e.getMessage());
		}
	}

	/**
	 * 增加
	 */
	@LogAble(operate = LogAble.OPERATE_ADD)
	public void adAdd() {
		int num = service.add(model);
		Long id = Sequence.getSequence();
		try {
			if (num == 0) {
				sendMsg(Constant.ADD_FALSE);
			} else if (num == 1) {
				sendMsg(Constant.ADD_SUCCESS);
			}
			logWriter(id);
		} catch (Exception e) {
			sendMsg(e.getMessage());
		}
	}

	// 发送json信息给页面
	private void sendMsg(String msg) {
		JsonObject result = new JsonObject();
		ResultJson resultJs = new ResultJson();
		resultJs.msg = msg;
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void logWriter(long id) {
		ControlLogCategory logCategory = new ControlLogCategory();
		logCategory.setId(String.valueOf(id));
		logCategory.setName(model.getName());
		logCategorysArrayList.add(logCategory);
	}

	// 设置广告投放的在表 sys_control的 id
	public String getActionId() {
		return CM_CONTROL_ID;
	}

	@Override
	public ArrayList<ControlLogCategory> getProduct() {
		return logCategorysArrayList;
	}
}
